
#include <gio/gio.h>
#include <stdio.h>
#include "battery.h"

batteryCab *proxy;


static gboolean test_status_handler(batteryCab *object, gint value, gpointer userdata)
{
    printf("test_status_handler invoked! %d \n",value);
    return TRUE;
}

int main(int argc, char const *argv[])
{
    GError *connerror = NULL;
    GError *proxyerror = NULL;
    GDBusConnection *conn;
    GMainLoop *loop;

    #if !GLIB_CHECK_VERSION(2,35,0)
    g_type_init();
    #endif
    
    conn = g_bus_get_sync(G_BUS_TYPE_SESSION,NULL,&connerror);

    if(connerror != NULL)
    {
        printf("g_bus_get_sync connect error! %s \n",connerror->message);
        g_error_free(connerror);
        return 0;
    }

    proxy = battery_cab_proxy_new_sync(conn,
                                    G_DBUS_PROXY_FLAGS_NONE,
                                    "cygia.battery.cab",
                                    "/cygia/battery/cab",
                                    NULL,
                                    &proxyerror);

    if(proxy == NULL)
    {
        printf("battery_cab_proxy_new_sync error! %s \n",proxyerror->message);
        g_error_free(proxyerror);
        return 0;
    }

    g_signal_connect(proxy,"test-status", G_CALLBACK(test_status_handler),NULL);

    GError *callError = NULL;
    battery_cab_call_set_name_sync(proxy, "ecb-a03", NULL, &callError);
    if(callError == NULL) {
        printf("battery_cab_call_set_name_sync sync success\n");
    }else {
        printf("call_set_version_sync error, %s\n",callError->message);
    }
    
    gchar *result = NULL;
    battery_cab_call_get_version_sync(proxy, &result, NULL, &callError);
    if(callError == NULL) {
        printf("battery_cab_call_get_version_sync success\n");
        printf("version: %s\n", result);
        g_free(result);
    } else {
        printf("call_set_version_sync error, %s\n",callError->message);
    }

    loop = g_main_loop_new(NULL, FALSE);
    g_main_loop_run(loop);

    g_object_unref(proxy);

    return 0;
}

